<?php
declare(strict_types=1);

namespace App\Controller\Backend;

use App\Logic\AdminUserLogic;
use Exception;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;

#[AutoController(prefix: '/user', server: 'backend')]
class UserController extends AbstractController
{
    /**
     * 用户列表
     * @return ResponseInterface
     */
    public function lists(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminUserLogic::listUser($input));
    }

    /**
     * 用户详情
     * @return ResponseInterface
     */
    public function detail(): ResponseInterface
    {
        $userId = (int)$this->request->input('userId', 0);
        if (!$userId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            return $this->apiReturn(200, 'succ', AdminUserLogic::detailUser($userId));
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 修改用户信息
     * @return ResponseInterface
     */
    public function edit(): ResponseInterface
    {
        $userId = (int)$this->request->input('userId', 0);
        if (!$userId) {
            return $this->apiReturn(400, '参数错误');
        }
        $input = $this->request->all();
        if (!intval($input['imId'])) {
            return $this->apiReturn(400, '聊天ID不能为空');
        }
        if (empty($input['nickname'])) {
            return $this->apiReturn(400, '昵称不能为空');
        }
        if (!empty($input['mobile']) && !is_mobile($input['mobile'])) {
            return $this->apiReturn(400, '手机号码格式不正确');
        }
        if (!empty($input['email']) && !is_email($input['email'])) {
            return $this->apiReturn(400, '邮箱格式不正确');
        }
        if (!empty($input['password']) && strlen($input['password']) < 6 || strlen($input['password']) > 20) {
            return $this->apiReturn(400, '请保持密码长度在6~20位');
        }
        try {
            AdminUserLogic::editUser($userId, $input);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 升级为助理
     * @return ResponseInterface
     */
    public function upgradeToAssistant(): ResponseInterface
    {
        $userId = (int)$this->request->input('userId', 0);
        if (!$userId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminUserLogic::upgradeToAssistant($userId);
            return $this->apiReturn(200, '升级成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 金币记录
     * @return ResponseInterface
     */
    public function coinRecords(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminUserLogic::coinRecords($input));
    }

    /**
     * 积分记录
     * @return ResponseInterface
     */
    public function integralRecords(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminUserLogic::integralRecords($input));
    }

    /**
     * 签到记录
     * @return ResponseInterface
     */
    public function signRecords(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminUserLogic::logSign($input));
    }

    /**
     * 任务记录
     * @return ResponseInterface
     */
    public function taskRecords(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminUserLogic::logTask($input));
    }

}